package zhibi.cms.controller.admin;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import zhibi.cms.domain.Site;
import zhibi.cms.extra.base.AdminBaseController;
import zhibi.cms.extra.context.Constant;
import zhibi.cms.extra.pojo.SitePo;
import zhibi.cms.service.SiteService;
import zhibi.commons.exception.MessageException;
import zhibi.frame.domain.Page;
import zhibi.frame.mybatis.example.Example;
import zhibi.frame.mybatis.example.ExampleType;

import java.util.List;
import java.util.Objects;

@Controller
@RequestMapping(Constant.SYS_PREFIX + "/admin/site")
public class CmsAdminSiteController extends AdminBaseController {

    @Autowired
    private SiteService siteService;

    /**
     * 列表
     *
     * @param model
     * @param page
     * @return
     */
    @RequestMapping("list")
    public String list(Model model, Page page, SitePo sitePo) {
        Example example = Example.getInstance()
                .addParam("s.name", sitePo.getName(), ExampleType.Operation.LIKE)
                .addOrder("s.sort", ExampleType.OrderType.DESC)
                .addOrder("s.pid", ExampleType.OrderType.DESC);
        PageInfo<SitePo> pageInfo = siteService.selectPoByExample(example, page);
        setModelAttribute(model, pageInfo);
        return Constant.SYS_PREFIX + "/site/list";
    }

    /**
     * 到添加页面
     *
     * @param model
     * @return
     */
    @GetMapping("add")
    public String add(Model model) {
        List<Site> siteList = siteService.selectAll();
        model.addAttribute("siteList", siteList);
        return Constant.SYS_PREFIX + "/site/add";
    }

    /**
     * 添加
     *
     * @return
     */
    @PostMapping("add")
    public String add(Site site) {
        if (site.getPid() != null && !Objects.equals(site.getPid(), "")) {
            Site pSite = siteService.selectByPrimaryKey(site.getPid());
            if (null == pSite) throw new MessageException("上级不存在");
        }
        site.setStatus(1);
        siteService.insertSelective(site);
        return redirect("list");
    }

    /**
     * 详情
     *
     * @param model
     * @param id
     * @return
     */
    @GetMapping("edit/{id}")
    public String edit(Model model, @PathVariable String id) {
        Site site = siteService.selectByPrimaryKey(id);
        if (null == site) throw new MessageException("该栏目不存在");
        List<Site> siteList = siteService.selectAll();
        model.addAttribute("siteList", siteList);
        model.addAttribute("site", site);
        return Constant.SYS_PREFIX + "/site/edit";
    }

    /**
     * 更新
     *
     * @param site
     * @return
     */
    @PostMapping("update")
    public String update(Site site) {
        if (site.getPid() != null && !Objects.equals(site.getPid(), "")) {
            Site pSite = siteService.selectByPrimaryKey(site.getPid());
            if (null == pSite) throw new MessageException("上级栏目不存在");
        }
        siteService.updateByPrimaryKeySelective(site);
        return refresh();
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping("del/{id}")
    public String del(@PathVariable String id) {
        Site site = siteService.selectByPrimaryKey(id);
        if (null == site) throw new MessageException("该栏目不存在");
        List<Site> siteList = siteService.selectByPid(site.getId());
        if (siteList != null && siteList.size() > 0) throw new MessageException("该栏目下面存在子菜单，不能删除");
        siteService.delete(site);
        return refresh();
    }

    /**
     * 更新菜单状态
     *
     * @param id
     * @return
     */
    @RequestMapping("updateStatus/{id}")
    public String updateStatus(@PathVariable String id) {
        Site site = siteService.selectByPrimaryKey(id);
        if (null == site) throw new MessageException("该栏目不存在");
        site.setStatus((site.getStatus() + 1) % 2);
        siteService.updateByPrimaryKeySelective(site);
        return refresh();
    }
}
